#!/bin/bash
#
# Copyright (C) 2013-2021 Spring Studio
# Copyright (C) 2013-2021 Dou Dongchun <doudongchun@163.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -e

# common defines
BUILD_ARCH=$1
BUILD_OPTION=$2

echo "BUILD_ARCH=$BUILD_ARCH"
echo "BUILD_OPTION=$BUILD_OPTION"
echo ""

ARCHS_32="armv7a x86"
ARCHS_64="armv7a arm64 x86 x86_64"
ARCHS_ALL=$ARCHS_64

echo_archs() {
    echo "========================================"
    echo "[*] check archs"
    echo "========================================"
    echo "ALL_ARCHS = $ARCHS_ALL"
    echo "ACT_ARCHS = $*"
    echo ""
}

echo_usage() {
    NAME=$(basename $0)
    echo "Usage:"
    echo "  $NAME armv7a|arm64|x86|x86_64"
    echo "  $NAME all|all32"
    echo "  $NAME all64"
    exit 1
}

init_env() {
  echo "========================================"
  echo "[*] config NDK $1 standalone toolchain"
  echo "========================================"
  . ./tools/do-detect-env.sh $1
}

do_anch()
{
  $STRIP --strip-debug --strip-unneeded ./build/$ABI/lib/*.so
}

case "$BUILD_ARCH" in
    armv5|armv7a|arm64|x86|x86_64)
        echo_archs $BUILD_ARCH $BUILD_OPTION
        init_env $BUILD_ARCH
        do_anch
    ;;
    all32)
        echo_archs $ARCHS_32
        for ARCH in $ARCHS_32
        do
            init_env $ARCH
            do_anch
        done
    ;;
    all|all64)
        echo_archs $ARCHS_64
        for ARCH in $ARCHS_64
        do
            init_env $ARCH
            do_anch
        done
    ;;
    *)
        echo_usage
        exit 1
    ;;
esac
